<?php

namespace App\Admin\Controllers;

use App\Admin\Controllers\Helpers\LinkHelper;
use App\Models\BlockedUser;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class BlockedUserController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new BlockedUser(), function (Grid $grid) {
            $grid->quickSearch('id', 'user_id','user.uuid','user.nickname')->placeholder('输入 id\uuid\用户昵称\uid')->auto(false);;

            $grid->model()->with('user')->orderByDesc('updated_at');
            $grid->column('id')->sortable();
            $grid->column('user_id')->display(function () {
                return LinkHelper::displayUser($this->user);
            });
            $grid->column('reason');
            $grid->column('times')->sortable();
            $grid->column('action')->sortable();
            $grid->column('version')->label();
            $grid->column('created_at', '初次封禁时间');
            $grid->column('updated_at', '最后封禁时间')->sortable();

            $grid->setActionClass(Grid\Displayers\Actions::class);
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableDelete();
                $actions->disableEdit();
            });
            $grid->disableCreateButton();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('user_id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new BlockedUser(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('reason');
            $show->field('times');
            $show->field('action');
            $show->field('version');
            $show->field('payload');
            $show->field('created_at');
            $show->field('updated_at');
            $show->disableDeleteButton();
            $show->disableEditButton();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new BlockedUser(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('reason');
            $form->text('times');
            $form->text('action');
            $form->text('payload');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
